// pages/index/explore/index.js
import {$get} from '../../utils/http.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list:[],
        tabArr:[
            {icon:'renwen',title:'正念冥想'},
            {icon:'yejing',title:'睡眠助手'},
            {icon:'yinle',title:'舒缓声音'},
            {icon:'renwen',title:'正念冥想'},
            {icon:'yejing',title:'睡眠助手'},
            {icon:'yinle',title:'舒缓声音'},
          ],
          tabIdx:0,   //当前被选中的菜单下标
          sId:'', //标记滚动位置
          cateList:[], //分类列表
          tabActive:0,
          tabsShow:true,
          lv1:'', //存储一级类目名称
          lv2:'',
          page:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //加载分类
        getApp().fly.get('classes/ReactCategory').then(res=>{
            let {results} = res.data
            let parentArr = results.filter(item=>item.parentId=='0-0')
            parentArr.forEach(item=>{
                item.children = results.filter(child=>item.objectId==child.parentId)
            })
            this.setData({
                cateList:parentArr,
                lv1:parentArr[0].name
            })
            this.fetchData() //触发条件查询
        })
        
    },
    handleDetail(ev){
        let {idx} = ev.currentTarget.dataset
        //打开新页面
        wx.navigateTo({
          url: `/pages/detail/index?idx=${idx}`,
        })
        //重定向
        // wx.redirectTo({
        //   url: '/pages/detail/index',
        // })
        // 重载页面
        // wx.reLaunch({
        //   url: '/pages/expore/index',
        // })
    },
    //点击主菜单
    handleTab(ev){
        let {idx} = ev.currentTarget.dataset
        let sId = `tab${idx-1}` //获取当前被点击菜单id
        // this.selectComponent('#tabs').resize(); //触发子菜单重绘
        let {name} = this.data.cateList[idx]
        this.setData({
            tabIdx:idx,
            sId,
            tabsShow:false,
            lv1:name,
            lv2:'',
            page:0,
            list:[]
        })
        this.fetchData()
        setTimeout(()=>{
            this.setData({
                tabsShow:true
            })
        },10)
    },
    //点击子菜单
    handleVanTabs(ev){
        console.log(ev);
        let {title} = ev.detail
        this.setData({
            lv2:title,
            page:0,
            list:[]
        })
        this.fetchData()
    },
    //负责课程列表的请求
    fetchData(){
        let {lv1,lv2,page,list} = this.data
        let where = {lv1}
        if(lv2&&lv2!='全部'){
            where.lv2 = lv2
        }
        let skip = page*10
        getApp().fly.get('classes/ReactArticle',{
            where,
            limit:10,
            skip
        }).then(res=>{
            let {results} = res.data
            wx.stopPullDownRefresh() //关闭下拉刷新动画
            if(results.length){
                this.setData({
                    list:[
                        ...list, //原数据
                        ...results //新数据
                    ],
                })
                return
            }
            wx.showToast({
              title: '没有更多数据了',
              icon:'none'
            })
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log('下拉刷新');
        //下拉刷新后，让所有数据包恢复到初始状态
        this.setData({
            lv1:'正念冥想',
            lv2:'',
            list:[],
            page:0,
            tabIdx:0,
            tabsShow:false,
            sId:'tab0'
        })
        setTimeout(()=>{
            this.setData({
                tabsShow:true
            })
        },10)
        this.fetchData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log('触底了');
        let page = this.data.page+1
        this.setData({
            page
        })
        this.fetchData()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})